﻿using EnvDTE;
using Microsoft.VisualStudio.TemplateWizard;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BOTemplatesItemsWizards
{
    public class FormlyPageWizard : IWizard
    {
        private bool m_isCRUDPage = false;

        public void BeforeOpeningFile(EnvDTE.ProjectItem projectItem)
        {

        }

        public void ProjectFinishedGenerating(EnvDTE.Project project)
        {

        }

        public void ProjectItemFinishedGenerating(EnvDTE.ProjectItem projectItem)
        {
            if (!m_isCRUDPage)
            {
                if (projectItem.Name.Contains("_CRUD"))
                {
                    projectItem.Delete();
                }
            }
        }

        public void RunFinished()
        {

        }

        public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            //  Create the form.
            PageElementsSetting form = new PageElementsSetting();
            
            //  Show the form.
            form.ShowDialog();

            //  Add the options to the replacementsDictionary...
            replacementsDictionary.Add("$toolbar-area$", form.ToolbarArea);
            replacementsDictionary.Add("$footer-area$", form.FooterArea);
            replacementsDictionary.Add("$action-code$", form.ActionCode);
            replacementsDictionary.Add("$layout-code$", form.LayoutCode);
            replacementsDictionary.Add("$head-settings$", form.HeadSettings);
            replacementsDictionary.Add("$panel-header$", form.PanelHeader);
            replacementsDictionary.Add("$grid-action-code$", form.GridCRUDActionCode);
            replacementsDictionary.Add("$grid-definition$", form.GridDefinition);
            replacementsDictionary.Add("$controls-library-definition$", form.ControlsLibraryDefinition);

            m_isCRUDPage = form.IsGridCRUD;

            //if (form.IsGridCRUD)
            //{
            //    if (automationObject is DTE)
            //    {
            //        DTE dte = (DTE)automationObject;
            //        Array activeProjects = (Array)dte.ActiveSolutionProjects;

            //        if (activeProjects.Length > 0)
            //        {
            //            string codebase = typeof(FormlyPageWizard).Assembly.CodeBase;
            //            var uri = new Uri(codebase, UriKind.Absolute);
            //            string pp = uri.LocalPath;
            //            Console.WriteLine(pp);

            //            string m_pathType = System.IO.Path.GetDirectoryName(uri.AbsolutePath);
            //            string m_basePath = System.IO.Path.GetDirectoryName(m_pathType);
            //            m_basePath = Uri.UnescapeDataString(m_basePath);
            //            string m_itemTemplatePath = m_basePath + "\\ItemTemplates\\CSharp\\GrayParrot\\1033\\FormlyPageTemplate.zip";

            //            Project activeProj = (Project)activeProjects.GetValue(0);
            //            activeProj.ProjectItems.AddFromTemplate(m_itemTemplatePath, "GridPage_CRUD.aspx");
            //        }
            //    }
            //}

            //replacementsDictionary.Add("$PageTitle$", form.PageTitle);
            //replacementsDictionary.Add("$PanelTitle$", form.PanelTitle);
            //replacementsDictionary.Add("$AuthorizableItemGuid$", Guid.NewGuid().ToString());
        }

        public bool ShouldAddProjectItem(string filePath)
        {
            return true;
        }
    }
}
